<template>
<div class="assetswithdrawmian">
    <section class="withdraw">
        <ul class="withdrawheader">
            <li class="list">
                <h2>{{$t('widthdraw[0]')}}</h2>
            </li>
            <li class="list">
                <h2>{{$t('widthdraw[1]')}}</h2>
                <el-select v-model="formLabelAlign.wid" :placeholder="$t('msg[6]')" @change="typeChange">
                    <el-option v-for="(item, index) in rechargetype" :key="index" :label="item.code" :value="item.wid"></el-option>
                </el-select>
            </li>
        </ul>
        <section class="withdrawmian">
            <ul class="mian">
                <li class="list">
                    <h2 class="title">{{$t('header.route[8]')}}</h2>
                    <div class="from">
                        <div class="input">
                            <el-input v-model="formLabelAlign.address" :placeholder="$t('msg[7]')"></el-input>
                        </div>
                        <div class="input">
                            <el-select v-model="formLabelAlign.address" :placeholder="$t('msg[6]')">
                                <el-option v-for="(item, index) in tiAddres" :key="index" :label="item.address" :value="item.address"></el-option>
                            </el-select>
                        </div>
                    </div>
                </li>
                <li class="list">
                    <h2 class="title">{{$t('widthdraw[2]')}}</h2>
                    <div class="from">
                        <div class="input">
                            <el-input v-model="formLabelAlign.amount" type="number" :max="checkBalancedata.withdraw_max" :min="checkBalancedata.withdraw_min" :placeholder="$t('msg[7]')"></el-input>
                        </div>
                        <div class="input">
                            <p>{{$t('widthdraw[3]')}}{{checkBalancedata.balance}}</p>
                        </div>
                        <div class="input">
                            <p>{{$t('widthdraw[4]')}}</p>
                            <p>{{checkBalancedata.handling_fee}}</p>
                        </div>
                    </div>
                </li>
                <li class="list">
                    <h2 class="title">{{$t('widthdraw[5]')}}</h2>
                    <div class="from">
                        <div class="input">
                            <el-input v-model="formLabelAlign.payment_password" :placeholder="$t('msg[7]')" show-password></el-input>
                        </div>
                    </div>
                </li>
                <li class="list">
                    <h2 class="title">{{$t('widthdraw[6]')}}({{Hidephone($store.state.user.phone)}})</h2>
                    <div class="from">
                        <div class="input">
                            <el-input v-model="formLabelAlign.v_code" style="width: 70%" :placeholder="$t('msg[7]')"></el-input>
                            <el-button @click="Verificationfun()">{{Verification.text}}</el-button>
                        </div>
                    </div>
                </li>
                <li class="list" v-if="config == 1">
                    <h2 class="title">{{$t('widthdraw[7]')}}</h2>
                    <div class="from">
                        <div class="input">
                            <el-input v-model="formLabelAlign.google_code" :placeholder="$t('msg[7]')"></el-input>
                        </div>
                    </div>
                </li>
                <li class="list">
                    <div class="from">
                        <div class="input">
                            <el-button type="primary" @click="handleSubmit()">{{$t('widthdraw[8]')}}</el-button>
                        </div>
                    </div>
                </li>
                <li class="list">
                    <h2 class="title">{{$t('widthdraw[9]')}}</h2>
                    <p>{{$t('widthdraw[10]')}}</p>
                    <p>{{$t('widthdraw[11]')}}</p>
                </li>
            </ul>
        </section>
    </section>
</div>
</template>

<script>
import {
    checkBalance,
    getWithdrawAddress,
    withdrawCoinList,
    applyWithdraw
} from "@/api/assetstotal";
// import { mapActions } from 'vuex'
import {
    Passwordencryption,
    Hidephone
} from "@/libs/util";
import {
    details,
    assetInfo
} from "@/api/user";
export default {
    name: "assetswithdraw",
    data() {
        return {
            config: '',
            rechargetype: [],
            tiAddres: [],
            formLabelAlign: {
                wid: 0, //提币币种
                type: "1",
                address: "", //提现地址
                amount: "", //提币金额
                payment_password: "", //资金密码
                v_code: "", //短信验证码
                google_code: "", //谷歌验证码 如果开启
            },
            checkBalancedata: {
                balance: "00000.000000",
                withdraw_min: 0.0,
                withdraw_max: 10.0,
                handling_fee: "0.00000000",
            },
            Verification: {
                text: this.$t("reg[0]"),
                time: 60,
                type: true,
                timer: "",
            },
        };
    },
    methods: {
        Hidephone(data) {
            return Hidephone(data);
        },
        Verificationfun() {
            if (!this.Verification.type) {
                return false;
            }
            var _data = {
                type: "4",
                phone: this.$store.state.user.phone ?
                    this.$store.state.user.phone :
                    this.$store.state.user.email,
            };
            this.$public.captcha(_data, this);
        },
        typecaptcha() {
            this.Verification.type = false;
            this.Verification.timer = setInterval(() => {
                this.Verification.time--;
                if (this.Verification.time < 1) {
                    clearInterval(this.Verification.timer);
                    this.Verification.type = true;
                    this.Verification.time = 60;
                    this.Verification.text = this.$t("reg[1]");
                } else {
                    this.Verification.text = this.Verification.time + "S";
                }
            }, 1000);
        },
        typeChange(i) {
            this.formLabelAlign.address = "";
            this.getdata();
        },
        handleSubmit() {
            let _Data = JSON.parse(JSON.stringify(this.formLabelAlign));
            applyWithdraw(Passwordencryption(_Data)).then((res) => {
                if (res.data.status_code == 200) {
                    this.$message({
                        message: this.$t("total[12]"),
                        type: "success",
                    });
                    setTimeout(() => {
                        this.$router.push({
                            path: "/home"
                        });
                    }, 800);
                } else {
                    this.$message.error(res.data.message);
                }
            }).catch((err) => {});
        },
        getdata() {
            checkBalance({
                wid: this.formLabelAlign.wid,
                account: "1", //币币账户
            }).then((res) => {

                this.checkBalancedata = res;

            }).catch((err) => {});
            getWithdrawAddress()
                .then((res) => {

                    let list = res;
                    for (let item of list) {
                        if (item.wid == this.formLabelAlign.wid) {
                            this.tiAddres = item.list;
                        }
                    }

                }).catch((err) => {});
        },
    },
    beforeEach: function (to, from, next) {
        clearInterval(this.Verification.timer);
        next();
    },
    created() {
        withdrawCoinList().then((res) => {

            this.rechargetype = res;
            this.formLabelAlign.wid = res[0].wid;
            this.getdata();

        });
        // 判断开启了google验证
        details().then((res) => {

            this.config = res.config.google_verify;

        });
    },
};
</script>
